/**
 * Licensed under the Apache License Version 2.0.
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.cg.cfg;

import net.interknit.knitter.cg.cfg.CGConfig.RequestParamType;
import net.interknit.knitter.cg.util.Utils;

import org.jdom.Element;

/**
 *
 */
public class MethodEl
{
	enum RestMethod
	{
		GET,
		PUT,
		POST,
		DELETE;

		static RestMethod parse(String value)
		{
			try
			{
				return valueOf(value);
			}
			catch(Exception e)
			{
				return null;
			}
		}
	}

	private String methodName;
	private String[] parameters = new String[0];
	private boolean exclude = false;
	private String restName;
	private String consumes;
	private String produces;
	private RestMethod restMethod;
	private RequestParamType requestParamType = RequestParamType.PATH;
	private String[] parameterNames = new String[0];
	private String comments = "";

	MethodEl()
	{
		;
	}

	/**
	 * @return the methodName
	 */
	public String getMethodName()
	{
		return methodName;
	}

	/**
	 * @return the parameters
	 */
	public String[] getParameters()
	{
		return parameters;
	}

	/**
	 * @return the exclude
	 */
	public boolean isExclude()
	{
		return exclude;
	}

	/**
	 * @return the restName
	 */
	public String getRestName()
	{
		return restName;
	}

	/**
	 * @return the consumes
	 */
	public String getConsumes()
	{
		return consumes;
	}

	/**
	 * @return the produces
	 */
	public String getProduces()
	{
		return produces;
	}

	/**
	 * @return the restMethod
	 */
	public RestMethod getRestMethod()
	{
		return restMethod;
	}

	/**
	 * @return the parameterNames
	 */
	public String[] getParameterNames()
	{
		return parameterNames;
	}

	/**
	 * @return the comments
	 */
	public String getComments()
	{
		return comments != null && ! "".equals(comments) ? comments : "";
	}

	public RequestParamType getRequestParamType()
	{
		return requestParamType;
	}

	public MethodEl(Element el)
	{
		if(el == null) return;

		String val = el.getAttributeValue("methodname");
		methodName = val;

		val = el.getAttributeValue("parameters");
		String[] sarr = val != null ? val.split(",") : new String[0];

		parameters = sarr != null ? new String[sarr.length] : null;
		for(int i = 0; i < sarr.length; i++)
		{
			String param = sarr[i].trim();
			if(param.endsWith(".class"))
				param = param.substring(0, param.lastIndexOf(".class"));
			parameters[i] = Utils.cleanWhiteSpaces(param);
		}

		val = el.getAttributeValue("exclude");
		exclude = new Boolean(val).booleanValue();

		val = el.getAttributeValue("restname");
		restName = val != null ? val : methodName;

		consumes = el.getAttributeValue("consumes");

		produces = el.getAttributeValue("produces");

		val = el.getAttributeValue("restmethod");
		restMethod = val != null && ! "".equals(val) ? RestMethod.parse(val) : null;

		val = el.getAttributeValue("paramtype");
		if(val != null && ! val.isEmpty())
		{
			requestParamType = RequestParamType.parse(val);
		}
		else
		{
			if(new Boolean(el.getAttributeValue("useasqueryparams")).booleanValue())
				requestParamType = RequestParamType.QUERY;
			else
				requestParamType = RequestParamType.PATH;
		}

		val = el.getAttributeValue("paramnames");
		parameterNames = val != null ? val.split(",") : new String[0];

		for(int i = 0; i < parameterNames.length; i++)
			parameterNames[i] = Utils.cleanWhiteSpaces(parameterNames[i]);

		comments = el.getChildTextTrim("comments");
	}

	@Override
	public String toString()
	{
		String p = "";
		String pn = "";

		if(getParameters() != null)
			for(String s : getParameters()) p += s + ",";
		if(getParameterNames() != null)
			for(String s : getParameterNames()) pn += s + ",";

		return
		"<method " +
		"methodname=\"" + getMethodName() + "\" " +
		"parameters=\"" + p + "\" " +
		"exclude=\"" + isExclude() + "\" " +
		"restname=\"" + getRestName() + "\" " +
		"restmethod=\"" + getRestMethod() + "\" " +
		"requestParamType=\"" + getRequestParamType() + "\" " +
		"paramnames=\"" + pn + "\" " + ">" +
		"<comments>" + getComments() + "</comments>" +
		"</method>";
	}
}
